package com.agv.backend.service.impl;


import com.agv.backend.entity.AgvMovementResponse;
import com.agv.backend.service.AgvMovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class AgvMovementServiceImpl implements AgvMovementService {

    @Autowired
    private RestTemplate restTemplate;

    private final String url = "http://192.168.2.57/prod-api/agv/movement";

    @Override
    public AgvMovementResponse getHeartbeat(){
        ResponseEntity<AgvMovementResponse> agvMovementResponseResponseEntity = restTemplate.getForEntity(url+"/heartbeat", AgvMovementResponse.class);
        AgvMovementResponse agvMovementResponse = agvMovementResponseResponseEntity.getBody();
        if("操作成功".equals(agvMovementResponse.getMsg())){
            return agvMovementResponse;
        }

        AgvMovementResponse agvMovementResponseFailure = new AgvMovementResponse();
        agvMovementResponseFailure.setMsg("操作失败");
        return agvMovementResponseFailure;
    }

    @Override
    public Boolean forward() {
        ResponseEntity<AgvMovementResponse> responseResponseEntity = restTemplate.postForEntity(url + "/forward",null, AgvMovementResponse.class);
        AgvMovementResponse agvMovementResponse = responseResponseEntity.getBody();

        Boolean bool = false;

        if("操作成功".equals(agvMovementResponse.getMsg())){
            bool = true;
        }

        return bool;

    }

    @Override
    public Boolean stop() {
        ResponseEntity<AgvMovementResponse> agvMovementResponseResponseEntity = restTemplate.postForEntity(url+"/stop",null, AgvMovementResponse.class);
        AgvMovementResponse agvMovementResponse = agvMovementResponseResponseEntity.getBody();
        Boolean bool = false;

        if("操作成功".equals(agvMovementResponse.getMsg())){
            bool = true;
        }

        return bool;
    }

    @Override
    public Boolean backward() {

        ResponseEntity<AgvMovementResponse> agvMovementResponseResponseEntity = restTemplate.postForEntity(url+"/backward",null, AgvMovementResponse.class);
        AgvMovementResponse agvMovementResponse = agvMovementResponseResponseEntity.getBody();
        Boolean bool = false;

        if("操作成功".equals(agvMovementResponse.getMsg())){
            bool = true;
        }

        return bool;
    }


}
